import 'dart:convert';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:pplive/models/extensions/userExtensions.dart';
import 'package:pplive/models/follow_model.dart';
import 'package:pplive/models/resp_model.dart';
import 'package:pplive/models/visit_model.dart';
import 'package:pplive/pages/mine/mine_page.dart';

import '../../common/api_service.dart';
import '../../models/user.dart';

class MineSubPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _MineSubState();
}

class _MineSubState extends State<MineSubPage> {
  late ThemeData theme;

  Future<RespModel<List<User>>?> fetchData() async {
    String api = Get.parameters["api"] as String;
    ApiService apiService = ApiService.fromJsonString(api);
    try {
      if (apiService.path == "/pr/friend/list") {
        var ret = await apiService.fetch<List<User>>();
        return ret;
      } else if (apiService.path == "/pr/list/fans") {
        var ret = await apiService.fetch<List<FollowModel>>();
        var users = ret?.data?.map((e) => e.user).toList().cast<User>();
        return RespModel(
            code: ret?.code ?? "", message: ret?.message ?? "", data: users);
      } else if (apiService.path == "/pr/list/follow") {
        var ret = await apiService.fetch<List<FollowModel>>();
        var users = ret?.data?.map((e) => e.user).toList().cast<User>();
        return RespModel(
            code: ret?.code ?? "", message: ret?.message ?? "", data: users);
      } else if (apiService.path == "/pu/visit/record/list") {
        var ret = await apiService.fetch<VisitModel>();
        var users =
            ret?.data?.recordList.map((e) => e.userInfo).toList().cast<User>();
        return RespModel(
            code: ret?.code ?? "", message: ret?.message ?? "", data: users);
      } else {
        return null;
      }
    } catch (e) {
      print("$e");
      return null;
    }
  }

  @override
  Widget build(BuildContext context) {
    theme = Theme.of(context);
    var item = Get.arguments as MineItem;
    String api = Get.parameters["api"] as String;
    debugPrint("api:$api");
    return Scaffold(
      backgroundColor: theme.colorScheme.onSurface,
      appBar: AppBar(
        title: Text(item.title),
      ),
      body: Padding(
        padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
        child: SafeArea(
            child: FutureBuilder(
                future: fetchData(),
                builder: (context, snapshot) {
                  var users = snapshot.data?.data ?? [];

                  return Padding(
                      padding: EdgeInsets.fromLTRB(0, 16, 0, 16),
                      child: ListView.builder(
                          itemCount: users.length,
                          itemBuilder: (context, index) {
                            return ListTile(
                              title: Text(users[index].name,
                                  style: TextStyle(
                                      fontFamily: "PingFang SC",
                                      fontSize: 14,
                                      fontWeight: FontWeight.w500,
                                      color: Colors.white)),
                              subtitle:users[index].createRichText(),








                              leading: Container(
                                width: 46,
                                height: 46,
                                child: ClipRRect(
                                    borderRadius: BorderRadius.circular(23),
                                    child: CachedNetworkImage(
                                        imageUrl: users[index].head)),
                              ),
                            );
                          }));
                })),
      ),
    );
  }
}
